import { http } from '@/utils/request';

export interface User {
  id: string;
  name: string;
  email: string;
  password: string;
  avatar?: string;
  position?: string;
  departmentId?: number;
  roleId?: number;
  phone?: string;
  status: number;
  gender?: number;
  birthday?: Date;
  bio?: string;
  role: string;
  lastLoginAt?: Date;
  loginCount: number;
  createdAt: Date;
  updatedAt: Date;
}

export interface CreateUserRequest {
  name: string;
  email: string;
  position?: string;
  phone?: string;
  gender?: number;
  status: number;
  bio?: string;
  role: string;
}

export interface UpdateUserRequest {
  id: string;
  name: string;
  email: string;
  position?: string;
  phone?: string;
  gender?: number;
  status: number;
  bio?: string;
  role: string;
}

export async function fetchUserPage() {
  const response = await http.post<User>('/api', {
    resource: 'system/user',
    action: 'find_page',
    version: 'v1',
    params: {},
    meta: {
      page: 1,
      size: 20,
    },
  });
  return response;
}

export async function createUser(data: CreateUserRequest) {
  const response = await http.post<User>('/system/user', data);
  return response;
}

export async function updateUser(data: UpdateUserRequest) {
  const response = await http.post<User>('/api', {
    resource: 'sys/user',
    action: 'login',
    version: 'v1',
    params: {
      type: 'update',
      data,
    },
    meta: {},
  });
  return response;
}

export async function deleteUser(id: string) {
  const response = await http.delete<void>(`/system/user/${id}`);
  return response;
}

export async function resetPassword(id: string) {
  const response = await http.post<void>(`/system/user/reset-password/${id}`);
  return response;
}

export interface ChangePasswordRequest {
  oldPassword: string;
  newPassword: string;
}

export async function changePassword(data: ChangePasswordRequest) {
  const response = await http.post<void>('/system/user/change-password', data);
  return response;
}
